package com.southwind.controller;


import com.southwind.entity.ProductInfo;
import com.southwind.service.IProductCategoryService;
import com.southwind.service.IProductInfoService;
import com.southwind.vo.ProductCategoryVO;
import com.southwind.vo.ResultVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 商品表 前端控制器 买家端
 * </p>
 *
 * @author admin
 * @since 2021-04-18
 */
@RestController
@RequestMapping("/buyer/product")
@Slf4j
public class ProductInfoBuyerController {

    @Autowired
    private IProductCategoryService productCategoryService;

    @Autowired
    private IProductInfoService productInfoService;

    @Value("${server.port}")
    private String port;

    @GetMapping("/list")
    public ResultVO<List<ProductCategoryVO>> list() {
        log.info("当前调用的product实例的端口号是：{}", port);
        return ResultVO.successWithData(productCategoryService.productCategoryVOList());
    }

    @GetMapping("/findById/{id}")
    public ProductInfo findById(@PathVariable Integer id) {
        return productInfoService.getById(id);
    }

    @PutMapping("/subStock/{id}/{quantity}")
    public Boolean subStock(@PathVariable Integer id, @PathVariable Integer quantity) {
        return productInfoService.subStock(id, quantity);
    }

    @GetMapping("/findAllProductCategory")
    public ResultVO<Map<String, List<ProductCategoryVO>>> findAllProductCategory() {
        List<ProductCategoryVO> productCategoryVOList = productCategoryService.onlyProductCategoryVOList();
        Map<String, List<ProductCategoryVO>> map = new HashMap<>();
        map.put("content", productCategoryVOList);
        return ResultVO.successWithData(map);
    }

    @PostMapping("/add")
    public ResultVO add(@RequestBody ProductInfo productInfo) {
        boolean b = productInfoService.save(productInfo);
        return ResultVO.auto(b);
    }
}

